package main.java.indi.zyj.c150.dp;

import java.util.List;

public class _13_MaximalSquare {


    public int maximalSquare(char[][] matrix) {

        if (matrix == null || matrix.length == 0 || matrix[0].length == 0){
            return 0;
        }

        int row = matrix.length, col = matrix[0].length;
        int[][] dp = new int[row][col];
        int maxSize = 0;

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (matrix[i][j] == '1'){
                    if (i == 0 || j == 0){
                        dp[i][j] = 1;
                    }else {
                        dp[i][j] = Math.min(Math.min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]) + 1;
                    }
                    maxSize = Math.max(maxSize, dp[i][j]);
                }
            }
        }

        return maxSize*maxSize;

    }

}
